package com.itheima.job;

import com.itheima.common.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Set;

@Component
public class ClearImageJob {

    @Autowired
    private JedisPool jedisPool;

    public void run(){
        System.out.println(new Date());
        System.out.println("清理图片开始...");
        //清理 垃圾图片
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();

            Set<String> sdiff = jedis.sdiff("SETMEAL_ALL", "SETMEAL_ADD");
            for (String fileName : sdiff) {
                QiniuUtils.delete(fileName);
                jedis.srem("SETMEAL_ALL",fileName);
                System.out.println("清理的图片："+fileName);
            }
            System.out.println("清理图片完成...");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }

    }
}
